const express = require('express');
const session = require('express-session');

// 本地数据文件
const userData = require('../db/user.json');

const router = express.Router();

router.use(session({
    secret:'0825',
    saveUninitialized:false,
    resave:false,
    cookie:{
        httpOnly:true,
        maxAge:1000*60
    }
}))


router.get('/login',(req,res)=>{
    // ejs
    res.render('./login.ejs');
})

router.post('/login',(req,res)=>{
    
    let {username,pwd} = req.body;

    let result =userData.find(item=>item.username===username&&item.pwd===pwd);

    // 有此用户
    if(result){
        req.session.username = username;
        res.redirect('/list');
    }else{
        // res.json({msg:'用户密码错误'});
        res.redirect('/login');
    }

})

router.get('/logout',(req,res)=>{
    
    req.session.destroy(()=>{
        res.render('./logout.ejs');
    });

})


module.exports = router;